package cn.net.isir.controller.common;

import cn.net.isir.service.UserRoleService;
import cn.net.isir.service.UserService;
import cn.net.isir.service2.*;
import cn.net.isir.common.config.WxConfig;
import cn.net.isir.common.sms.SMSUtils;
import cn.net.isir.dao.PermissionDao;
import cn.net.isir.dao.RoleDao;
import cn.net.isir.entity.User;
import cn.net.isir.utils.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liuxubo
 * @filename TestController.java
 * @package cn.net.isir.controller
 * @email lxb@isir.net.cn
 * @date 2018/11/09
 * @comment
 * @since 1.8
 */
@Controller
@RequestMapping("/test")
public class TestController {
    // 声明log4j日志
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(TestController.class);

    @Resource
    private UserService userService;
    @Resource
    private TableService tableService;
    @Resource
    private PermissionDao permissionDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private TestService testService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private ShopService shopService;

//    @Autowired
//    private ShiroFilterFactory shiroFilterFactoryBean;

    @RequestMapping(method = RequestMethod.GET, value = "/i")
    public String testget(HttpServletRequest request, HttpServletResponse response) {
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (Cookie cookie : cookies) {
//                //http://127.0.0.1:8080/login.html;jsessionid=6F6D1F4B691139F0ECE05F1D8A6FE582
//                logger.info("------------------------------------");
//                logger.info(cookie.getName());
//                logger.info(cookie.getValue());
//            }
//        }
////        response.addCookie(new Cookie(StringUtils.randomByTime2(), StringUtils.randomByTime()));
//        int tableCount = tableService.selectCount(null);
//        logger.info(tableCount);
//        User user = new User();
//        permissionDao.selectPermissionListByUserId(user);
//        roleDao.selectListByUserId(user);
//        logger.info(WxConfig.getSub_appid());
//        testService.test();
//        userRoleService.selectRoleListByUserId(null);
//        shopService.delete("asdfasdf");
//        userService.selectCount(null);
        String m = request.getParameter("m");
        if (m == null) {
            m = "15069107340";
        }
//        SMSSender.sendVerCode(m, StringUtils.randomUUIDUpperCase(6));
        SMSUtils.sendVerCode(m, StringUtils.randomUUIDUpperCase(6));
        return "test";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/i")
    public String testpost(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                //http://127.0.0.1:8080/login.html;jsessionid=6F6D1F4B691139F0ECE05F1D8A6FE582
                logger.info("------------------------------------");
                logger.info(cookie.getName());
                logger.info(cookie.getValue());
            }
        }
//        response.addCookie(new Cookie(StringUtils.randomByTime2(), StringUtils.randomByTime()));
        int tableCount = tableService.selectCount(null);
        logger.info(tableCount);
        User user = new User();
        permissionDao.selectPermissionListByUserId(user);
        roleDao.selectListByUserId(user);
        logger.info(WxConfig.getSub_appid());
        testService.test();
        userRoleService.selectRoleListByUserId(null);
        shopService.delete("asdfasdf");
        userService.selectCount(null);
        return "test";
    }

    @RequestMapping("/chain")
    public String chain() {
//        ShiroFilterFactory shiroFilterFactory = (ShiroFilterFactory) shiroFilterFactoryBean;
//        shiroFilterFactory.createFilterChainManager();
        return null;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/t/{id}")
    public void tGet(@PathVariable String id) {
        logger.info("tGet");
        logger.info(id);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/t/")
    public void tPOST(@PathVariable String id) {
        logger.info("tPOST");
        logger.info(id);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/t/{id}")
    public void tPUT(@PathVariable String id) {
        logger.info("tPUT");
        logger.info(id);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/t/{id}")
    public void tDELETE(@PathVariable String id) {
        logger.info("tDELETE");
        logger.info(id);
    }
}
